package com.xuanwei.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.xuanwei.core.annos.Positive;
import com.xuanwei.core.entity.vo.CommentStatistic;
import com.xuanwei.core.entity.vo.CommentVo;
import com.xuanwei.core.entity.vo.ProductCommentVo;
import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwOrderCommentService;
import com.xuanwei.core.util.ParamUtil;
import com.xuanwei.xwcommon.consts.ProjectUrl;
import com.xuanwei.xwcommon.entity.Vo.ProductVo;
import com.xuanwei.xwcommon.entity.dto.R;
import com.xuanwei.xwcommon.entity.dto.SimpleProductDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/product")
@Slf4j
@Validated
public class ProductController extends BasicController {

    private RestTemplate restTemplate;

    private IXwOrderCommentService orderCommentService;

    public ProductController(RestTemplate restTemplate, IXwOrderCommentService orderCommentService) {
        this.restTemplate = restTemplate;
        this.orderCommentService = orderCommentService;
    }


    @RequestMapping(value = "productComment", method = RequestMethod.GET)
    public ResultVO productComment(@Positive(message = "ID参数错误") Integer productId) {
        ProductCommentVo productCommentVo = orderCommentService.findProductCommentVo(productId);
        return ResultVO.successResult(productCommentVo);
    }

    @RequestMapping(value = "commentStatistic", method = RequestMethod.GET)
    public ResultVO commentStatistic(@Positive(message = "ID参数错误") Integer productId) {
        CommentStatistic commentStatistic = orderCommentService.findCommentStatistic(productId);
        return ResultVO.successResult(commentStatistic);
    }

    @RequestMapping(value = "commentList", method = RequestMethod.GET)
    public ResultVO commentList(@Positive(message = "ID参数错误") Integer productId, Integer type) {
        List<CommentVo> commentList = orderCommentService.findCommentList(type, productId, getPage());
        return ResultVO.successResult(commentList);
    }

    /**
     * 商品详情
     */
    @RequestMapping(value = "detail", method = RequestMethod.GET)
    public ResultVO detail(@Positive(message = "ID参数错误")Integer id) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("ID参数错误");
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        String url = ProjectUrl.SEARCH + "es/detail?id={id}";
        ResponseEntity<R> forEntity = restTemplate.getForEntity(url, R.class, param);
        R body = forEntity.getBody();
        if (body != null) {
            String data = JSONObject.toJSONString(body.get("data"));
            ProductVo productVo = JSONObject.parseObject(data, ProductVo.class);
            return ResultVO.successResult(productVo);
        } else {
            return ResultVO.failResult("没有该商品");
        }
    }

    /**
     * 搜索商品
     *
     * @param name       商品名称
     * @param categoryId 类目ID
     * @param orderBy    排序字段
     * @param page       当前页
     * @param pageSize   每页大小
     * @param order      排序1升序2
     * @return 数据
     */
    @RequestMapping(value = "search", method = RequestMethod.GET)
    public ResultVO searchP(String name, String categoryId,
                            Integer orderBy, Integer page,
                            Integer pageSize, Integer order) {
        List<SimpleProductDto> search = search(name, categoryId, orderBy, page, pageSize, order);
        return ResultVO.successResult(search);
    }

    /**
     * 远程调用搜索
     *
     * @param name       商品名称
     * @param categoryId 类目ID
     * @param orderBy    排序字段
     * @param page       当前页
     * @param pageSize   每页大小
     * @param order      排序1升序2
     * @return 数据
     */
    private List<SimpleProductDto> search(String name, String categoryId,
                                          Integer orderBy,
                                          Integer page, Integer pageSize,
                                          Integer order) {
        Map<String, Object> param = new HashMap<>();
        param.put("name", name);
        param.put("categoryId", categoryId);
        param.put("orderBy", orderBy);
        param.put("page", page);
        param.put("pageSize", pageSize);
        param.put("order", order);
        try {
            String url = ProjectUrl.SEARCH + "es/search?name={name}" +
                    "&categoryId={categoryId}" +
                    "&orderBy={orderBy}&page={page}" +
                    "&pageSize={pageSize}&order={order}";
            ResponseEntity<R> forEntity = restTemplate.getForEntity(url, R.class, param);
            R body = forEntity.getBody();
            return (List<SimpleProductDto>) body.get("data");
        } catch (Exception e) {
            log.warn("{}", e);
            return null;
        }
    }

    /**
     * 查询商品的详情
     *
     * @param id ID
     * @return 数据
     */
    public SimpleProductDto findById(Integer id) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        String url = ProjectUrl.SEARCH + "es/simpleDetail?id={id}";
        ResponseEntity<R> forEntity = restTemplate.getForEntity(url, R.class, param);
        R body = forEntity.getBody();
        if (body != null) {
            String data = JSONObject.toJSONString(body.get("data"));
            return JSONObject.parseObject(data, SimpleProductDto.class);
        } else {
            return null;
        }
    }
}
